package com.wr.controller;

import com.github.pagehelper.PageHelper;
import com.wr.model.Article;
import com.wr.model.RestResponse;
import com.wr.service.ArticleService;
import com.wr.service.PlateService;
import com.wr.utils.PageData;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author weirong
 * @Title:
 * @Description:
 * @date 2018/6/1118:04
 */
@RestController
public class ArticleController {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ArticleService articleService;

    @RequestMapping("blog/detail")
    public RestResponse<Article> detail(@RequestParam Integer id) {
        Article article = articleService.selectByPrimaryKey(id);

        return RestResponse.success(article);
    }

    @RequestMapping("blog/add")
    public RestResponse<Article> add(@RequestBody Article article) {
        try {
            articleService.insertSelective(article);
        } catch (Exception e) {
            logger.error(e,e);
        }

        return RestResponse.success(article);
    }

    /**
     * @desc 首页文章列表
     * @return
     */
    @RequestMapping(value = "blog/mainList")
    public RestResponse<List<Article>> mainList(@RequestParam Integer pageNo,@RequestParam Integer pageSize) {
        if(pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        List<Article> articles = articleService.listPage(new Article(),pageNo);

        return RestResponse.success(articles);
    }
    @RequestMapping(value = "blog/listPd")
    public RestResponse<List<Article>> listPd(@RequestBody PageData pd) {
        List<Article> articles = articleService.listPd(pd);
        return RestResponse.success(articles);
    }

}
